#ifndef DEVICE_TFT_H
#define DEVICE_TFT_H
#include <SPI.h>
#include <TFT_eSPI.h>
#include <map>
#include "font.h"

#define TFT_CS_PIN 15   //CS  <==> GPIO15 <==> D8
#define TFT_RST_PIN 5   //RST <==> GPIO05 <==> D1
#define TFT_DC_PIN 4    //RS  <==> GPIO04 <==> D2
#define TFT_WIDTH 128
#define TFT_HIGHT 160
#define OPTION_NUM 4
#define OPTION_PER_MAX 4
#define CHAR_WIDTH 16
#define OPTION_SLOT (CHAR_WIDTH / 2)
#define TFT_X_STA ((TFT_WIDTH-OPTION_PER_MAX*CHAR_WIDTH) / 2)
#define TFT_Y_STA ((TFT_HIGHT-OPTION_NUM*CHAR_WIDTH-OPTION_SLOT*(OPTION_NUM-1)) / 2)

// 按键名称
enum class PAGE_NAME {
    PAGE_HOME = 0,
    PAGE_TEMP,
    PAGE_HUMI,
    PAGE_ABOUT,
    PAGE_DEFAULT = 0xFFFF
};

class TFTControl {
    // 页面路由
    struct PageRoute
    {
        std::function<void()> page{nullptr};
        std::function<void()> keyUp{nullptr};
        std::function<void()> keyDown{nullptr};
        std::function<void()> keyOk{nullptr};
        uint16_t optionNum{0};
        uint16_t currOptiion{0};
        bool setOk{false};
        uint16 setValue{0};
    };
    
public:
    ~TFTControl();
    void init(void);
private:
    void pageHome();
    void pageTemp();
    void pageHumi();
    void displayChineseChar(uint16_t x, uint16_t y, uint16_t textColor, uint16_t bgColor, const std::string font);
    void displayNumber(uint16_t x, uint16_t y, uint16_t textColor, uint16_t bgColor, const std::string font);
private:
    TFT_eSPI *m_tftCtl{nullptr};
    PAGE_NAME m_currentPage{0};
    std::map<PAGE_NAME, PageRoute> m_pageRouteMap;
};

#endif